package com.modules.controller.sys;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.common.constant.CommonConstant;
import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.vo.LoginUser;
import com.common.vo.Result;
import com.modules.dto.sys.resp.MenuDtoResp;
import com.modules.entity.sys.UserEntity;
import com.modules.service.sys.OrganizationService;
import com.modules.service.sys.UserService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


/**
 * 登录
 * @author yyq
 *
 */
@Controller
public class LoginController extends BaseController<UserEntity>{

	@Autowired
	private UserService userService;
	@Autowired
	private OrganizationService organizationService;

	
	/**TODO
	 * 跳转登录和跳转主页、路径跳转
	 * @return
	 */
	@GetMapping(value="/login")
	public ModelAndView login(){
		return new ModelAndView( "login");
	}
	@GetMapping(value="/jumpMain")
	public ModelAndView index(){
		return new ModelAndView("main");
	}
	@GetMapping(value="/load")
	public ModelAndView load(){
		return new ModelAndView("load");
	}
	@GetMapping(value="views/{a}")
	public ModelAndView commonsUrl(@PathVariable("a") String a){
		return new ModelAndView(a);
	}
	@GetMapping(value="views/{a}/{b}")
	public ModelAndView commonsUrl2(@PathVariable("a") String a,@PathVariable("b") String b, HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView(a + "/" + b);
		setPower(modelAndView, "views/" + a + "/" + b, request);
		return modelAndView;
	}
	@GetMapping(value="views/{a}/{b}/{c}")
	public ModelAndView commonsUrl3(@PathVariable("a") String a,@PathVariable("b") String b,@PathVariable("c") String c, HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView(a + "/" + b + "/" + c);
		setPower(modelAndView, "views/" + a + "/" + b + "/" + c, request);
		return modelAndView;
	}

	public void setPower(ModelAndView modelAndView, String url, HttpServletRequest request){
		HttpSession session = request.getSession();
		List<MenuDtoResp> list = (List<MenuDtoResp>) session.getAttribute("powerList");
		Map<String, Object> map = MapUtil.newHashMap();
		if (CollUtil.isNotEmpty(list)){
			list.stream().forEach(vo -> {
				if(StrUtil.isNotEmpty(vo.getUrl()) && vo.getUrl().equals(url)){
					map.put(vo.getOperation(), true);
				}
			});
		}
		modelAndView.addObject("power", map);
	}

	/**
	 * 登录
	 * @return
	 */
	@LogData(module = "登录操作", remarks = "成功登录")
	@PostMapping(value="/loginJudge")
	@ResponseBody
	public Result<?> loginJudge(UserEntity user, HttpServletRequest request){
		if(StrUtil.isEmpty(user.getUserName()) || StrUtil.isEmpty(user.getPassword()) || StrUtil.isEmpty(user.getCaptcha())){
			return Result.fail();
		}
		//对验证码进行验证
		if (!CaptchaUtil.ver(user.getCaptcha(), request)) {
			return Result.fail("验证码不正确");
		}
		CaptchaUtil.clear(request);  // 清除session中的验证码
		return userService.selectLogin(user);
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@GetMapping(value="/loginExit")
	public ModelAndView loginExit(){
		removeUser();
		return new ModelAndView("login");
	}
	
	/**
	 * 页面主题
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "修改主题")
	@PostMapping(value="/main/theme")
	@ResponseBody
	public Result<?> theme(UserEntity user){
		user.setId(getUser().getId());
		userService.updateById(user);
		if(StrUtil.isNotBlank(user.getTheme())) {
			setSessionValue("theme", user.getTheme());
			String ts = (String) getSessionValue("themeStyle");
			String module = "module_theme0";
			if(StrUtil.isNotBlank(ts)) {
				if(ts.indexOf("module1") != -1) {
             		module = "module_"+user.getTheme();
             	}
			}
			setSessionValue("moduleTheme", module);
		}
		if(StrUtil.isNotBlank(user.getThemeStyle())) {
			setSessionValue("themeStyle", user.getThemeStyle());
			String module = "module_theme0";
			if(StrUtil.isNotBlank(user.getThemeStyle())) {
				if(user.getThemeStyle().indexOf("module1") != -1) {
             		module = "module_"+getSessionValue("theme");
             	}
			}
			setSessionValue("moduleTheme", module);
		}
		return Result.success();
	}
	
	/**TODO
	 * 锁屏
	 * @return
	 */
	@PostMapping(value="/main/suoping")
	@ResponseBody
	public Result<?> suoPing(String suoping, String mark){
		UserEntity user = new UserEntity();
		user.setPassword(suoping);
		user.setUserName(getUser().getUserName());
		return userService.suoPing(mark, user);
	}

	/**
	 * 个人信息
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "修改密码")
	@PostMapping(value="/main/updatePassWord")
	@ResponseBody
	public Result<?> updatePassWord(HttpServletRequest request){
		String id = request.getParameter("id");
		String oldPass = request.getParameter("oldPass");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		return userService.updatePassWord(id, oldPass, password1, password2);
	}

	/**
	 * 个人信息
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "修改个人信息")
	@PostMapping(value="/main/basicData")
	@ResponseBody
	public Result<?> basicData(HttpServletRequest request){

		return Result.success();
	}
	
	/**
	 * 清除缓存
	 * @return
	 */
	@LogData(module = "个人中心", remarks = "清除缓存")
	@PostMapping(value="/main/clearCache")
	@ResponseBody
	public Result<?> clearCache(HttpServletRequest request){
		
//		redisUtil.clear();
		
		return Result.success("清除成功");
	}
	
	/**
	 * 切换组织
	 * @return
	 */
	@PostMapping(value="/main/switchOrg")
	@ResponseBody
	public Result<?> switchOrg(Integer oid){
		LoginUser loginUser = getUser();
		loginUser.setLastOid(oid);
		setSessionValue(CommonConstant.LOGIN_USER, loginUser);
		UserEntity user = new UserEntity();
    	user.setLastOid(oid);
    	user.setId(loginUser.getId());
		userService.updateById(user);
		return Result.success("切换成功");
	}
	
	
	
	

}
